#include<stdio.h>
#include<stdlib.h>
int size;
int *A;
void append(newel)
{	
	
	A=realloc(A,sizeof(int)*(size+1));
	A[size]=newel;
	size=size+1;
}
void extend(int exsize)
{	int c[exsize];
	int i,j;
	for(i=0;i<exsize;i++)
	{	printf("enter the element at pos %d : \n\n",i);
		scanf("%d",&c[i]);
	}
	for(j=0;j<exsize;j++)
	{	append(c[j]);
	}
}
		
void displayatpos()
{ 	int pos;
	printf("enter the position : ");
	scanf("%d",&pos);
	printf("value at %dth position is : %d\n\n ",pos,A[pos]);
}
void slicing()
{	int st,en,i;
	printf("enter the starting position : ");
	scanf("%d",&st);
	printf("enter the another position : ");
	scanf("%d",&en);
	if(en>size)
	  en=size;
	if((st<0)||(st>en))
	  printf("\nslicing is not possible:\n\n");
	for(i=st;i<en;i++)
	{	printf("%d  ",A[i]);
	}
}
void count()
{	int c,i,val;
	c=0;
        printf("\nEnter the value : ");
	scanf("%d",&val);
	for(i=0;i<size;i++)
	{
          if(val==A[i])	
              c++;
	}
	printf("total No. of elements in array are %d\n\n",c);
}
void findpos()
{	int num,i;
	printf("enter the number whose index is to be found : ");
	scanf("%d",&num);
	for(i=0;i<size;i++)
	{	if(A[i]==num)
		{	printf("element is found at : %d postion\n\n",i);
			break;
		}
	}
}
void reverse()
{
	int B[size],i,j,k;
	for(i=size-1,j=0;i<=0,j<size;i--,j++)
	{	B[j]=A[i];
	}
	for(k=0;k<size;k++)
	{	A[k]=B[k];
	}
	
}
void sorting()
{	int temp,i,j;
	for(i=0;i<size;i++)
	{	for(j=0;j<(size-1)-i;j++)
		{	if(A[j]>A[j+1])
			{	temp=A[j];
				A[j]=A[j+1];
				A[j+1]=temp;
			}
		}
	}
}
void pop()
{	int pos,i;
	printf("enter the pos : ");
	scanf("%d",&pos);
	printf("Value at position %d :%d \n",pos,A[pos]);
	if(pos!=size)
	{
		for(i=pos;i<size;i++)
		{ A[i]=A[i+1];
		}
		A=realloc(A,sizeof(int)*(size-1));
        	size=size-1;
	}
	else
	{	A=realloc(A,sizeof(int)*(size-1));
		size=size-1;
	}
}
void rem()
{	int num,pos,i;
	printf("enter the number : \n\n");
	scanf("%d",&num);
	for(i=0;i<size;i++)
	{	if(A[i]==num)
		{	pos=i;
			break;
		}
	}
	for(i=pos;i<size;i++)
		{ A[i]=A[i+1];
		} 
		A=realloc(A,sizeof(int)*(size-1));
		size=size-1;
	
}	
void insertatgiv()
{	int num,pos,i;
	printf("enter the pos : \n\n");
	scanf("%d",&pos);
	printf("enter the number : \n\n");
	scanf("%d",&num);
	for(i=size;i!=pos-1;i--)
     	A[i]=A[i-1];
   	A[pos]=num;
	A=realloc(A,sizeof(int)*(size+1));
	size+=1;
}
void display()
{	int i;
	printf("[");
	for(i=0;i<size;i++)
	{	printf("%d",A[i]);
		printf(" ");
	}
	printf("]");
	printf("\n\n");
}
void main()
{	int i=0,ch,choice;
	choice=1;
	char want;
	want='y';
	while(want=='y')
	{	A=realloc(A,sizeof(int)*(i+1));
		printf("enter the : %d element",i+1);
		scanf("%d",&A[i]);
		i=i+1;
		printf("want to enter more press y else press another key:\n");
		scanf("%s",&want);
	}
	size=i;
	while(choice==1)
	{
	printf("Enter your choice \n 1.append\n 2.extend\n 3.display element at given position \n 4.slicing \n 5.count \n 6.indexing \n 7.reverse \n 8.sorting \n 9.pop \n 10.remove \n 11.inseratgiv");
	scanf("%d",&ch);
	switch(ch)
	{	case 1 : {	int newel;
				printf("enter the new element to append in the array");
				scanf("%d",&newel);
				append(newel);
			 	display();
			 	break;
			 }
		case 2 : {	int exsize;
			 	printf("enter number of elements you want to append");
				scanf("%d",&exsize);
			 	extend(exsize);
			 	display();
			 	break;
			 }
		case 3 : displayatpos();
			 break;
		case 4 : slicing();
			 break;
		case 5 : count();
			 break;
		case 6 : findpos();
			 break;
		case 7 : reverse();
			 display();
			 break;
		case 8 : sorting();
			 display();
			 break;
		case 9 : pop();
			 display();
			 break;
		case 10 : rem();
			 display();
			 break;
		case 11: insertatgiv();
			 display();
			 break;
		}
		printf("Want to do more operations press 1 else other Key to exit");
		scanf("%d",&choice);
	}
}
